package com.friend.chat.utils

import android.app.Activity
import android.view.WindowManager
import android.view.inputmethod.InputMethodManager
import android.widget.EditText

/**
 * @des
 * @author batie fancy
 * @date 2023/1/13
 */
/**
 * 关闭当前的界面的软键盘
 */
fun hideKeyboard(activity: Activity?) {
    if (activity == null || activity.isFinishing) {
        return
    }
    try {
        val imm = activity
            .getSystemService(Activity.INPUT_METHOD_SERVICE) as InputMethodManager
        val view = activity.currentFocus
        if (view != null) {
            imm.hideSoftInputFromWindow(view.windowToken, 0)
        }
    } catch (e: Exception) {
        e.printStackTrace()
    }
}

/**
 * 隐藏软键盘
 */
fun showKeyboard(activity: Activity?,edtView:EditText) {
    if (activity == null || activity.isFinishing) {
        return
    }
    try {
        if (activity.window.attributes.softInputMode !== WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE) {

            val imm = activity
                .getSystemService(Activity.INPUT_METHOD_SERVICE) as InputMethodManager
            if (activity.currentFocus != null) {
                imm.showSoftInput(edtView, 0)
            }
        }
    } catch (e: Exception) {
    }
}